package ctrs.controller;

import com.alibaba.fastjson.JSON;
import ctrs.bean.Course;
import ctrs.bean.QueryInfo;
import ctrs.dao.CourseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class CourseController {

    @Autowired
    CourseDao courseDao;

    @CrossOrigin
    @RequestMapping("/allCourse")
    public String getAllCourse(QueryInfo queryInfo){
        System.out.println(queryInfo);
        List<Course> course = courseDao.getAllCourse("%"+queryInfo.getQuery()+"%");
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",course);
        System.out.println("res:"+ res);
        String course_json = JSON.toJSONString(res);
        return course_json;
    }

    @RequestMapping("/addCourse")
    public String addCourse(@RequestBody Course course){
        int i = courseDao.addCourse(course);
        return i>0?"success":"error";
    }

    @RequestMapping("/getUpdateCourse")
    public String getUpdateCourse(String courseId) {
        System.out.println("编号:" + courseId);
        Course updateCourse = courseDao.getUpdateCourse(courseId);
        String users_json = JSON.toJSONString(updateCourse);
        return users_json;
    }

    @RequestMapping("/editCourse")
    public String editCourse(@RequestBody Course course) {
        System.out.println(course);
        int i = courseDao.editCourse(course);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/deleteCourse")
    public String deleteCourse(String courseId){
        System.out.println(courseId);
        int i = courseDao.deleteCourse(courseId);
        String str = i > 0 ? "success" : "error";
        return str;
    }
}
